package cn.xuexiyuan.flinkstudy.transformation;

import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @Description:  重平衡（rebalance）分区
 *
 * @Author 左龙龙
 * @Date 21-3-23
 * @Version 1.0
 **/
public class TransformationDemo04 {
    public static void main(String[] args) throws Exception {
        // 0.env
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        // 1.source
        DataStreamSource<Long> longDS = env.fromSequence(0, 100);
        // 下面操作相当于将数据随机分配一下,有可能出现数据倾斜
        SingleOutputStreamOperator<Long> filterDS = longDS.filter(new FilterFunction<Long>() {
            @Override
            public boolean filter(Long aLong) throws Exception {
                return aLong > 10;
            }
        });

        // 2.transformation
        // 没有经过 rebalance 有可能出现数据倾斜
        SingleOutputStreamOperator<Tuple2<Integer, Integer>> result1 = filterDS.map(new RichMapFunction<Long, Tuple2<Integer, Integer>>() {
            @Override
            public Tuple2<Integer, Integer> map(Long aLong) throws Exception {
                // 子任务id/分区编号
                int subtask = getRuntimeContext().getIndexOfThisSubtask();
                return Tuple2.of(subtask, 1);
            }
            // 子任务id/分区编号分组,并统计元素个数
        }).keyBy(t -> t.f0).sum(1).setParallelism(1);


        // 经过 rebalance 有可能出现数据倾斜
        SingleOutputStreamOperator<Tuple2<Integer, Integer>> result2 = filterDS.rebalance().map(new RichMapFunction<Long, Tuple2<Integer, Integer>>() {
            @Override
            public Tuple2<Integer, Integer> map(Long aLong) throws Exception {
                // 子任务id/分区编号
                int subtask = getRuntimeContext().getIndexOfThisSubtask();
                return Tuple2.of(subtask, 1);
            }
            // 子任务id/分区编号分组,并统计元素个数
        }).keyBy(t -> t.f0).sum(1).setParallelism(1);


        // 3.sink
        result1.print("default");
        result2.print("rebalance");

        // 4.excute
        env.execute("TransformationDemo04");

    }
}
